/*
 * StatusPanel.java
 *
 *  created: 8.11.2011
 *  charset: UTF-8
 *  license: MIT (X11) (See LICENSE file for full license)
 */
package cz.mp.k3bg.gui.component;

import java.util.ArrayList;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

/**
 * Komponenta {@code StatusPanel} zobrazuje stavové popisky.
 * Nahrazuje otravné "vyskakovací" dialogy.
 * 
 * @author Martin Pokorný
 * @version 0.1
 * @see StatusLabel
 */
public class StatusPanel extends JPanel {
    
    private ArrayList<StatusLabel> labels = new ArrayList<StatusLabel>();
    private int labelsVisibleCount = 0;


    /** */
    public StatusPanel() {
        initLayout();        
    }
    
    /** */
    private void initLabels() {
        hideAllLabels();
        labelsVisibleCount = 0;
    }

    /** */
    private void hideAllLabels() {
        for (StatusLabel label : labels) {
            label.setVisible(false);
        }
    }

    /** */
    private void initLayout() {
        this.setLayout(new MigLayout("wrap",
                "0[]0",
                "0[]rel"));
    }

    /** */
    public void clear() {
        initLayout();
        initLabels();
    }

    /**
     *
     * @param text
     */
    public void addInfo(String text) {
        add(text, StatusLabel.Type.INFO);
    }
    
    /**
     *
     * @param text
     */
    public void addOk(String text) {
        add(text, StatusLabel.Type.OK);
    }

    /**
     * 
     * @param text 
     */
    public void addNote(String text) {
        add(text, StatusLabel.Type.NOTE);
    }

    /**
     * 
     * @param text 
     */
    public void addTip(String text) {
        add(text, StatusLabel.Type.TIP);
    }

    /**
     * 
     * @param text 
     */
    public void addWarning(String text) {
        add(text, StatusLabel.Type.WARNING);
    }
    
    /**
     * 
     * @param text 
     */
    public void addError(String text) {
        add(text, StatusLabel.Type.ERROR);
    }

    /**
     * 
     * @param text
     * @param type 
     */
    private void add(String text, StatusLabel.Type type) {
        StatusLabel label;
        if (labels.size() <= labelsVisibleCount) {
            label = new StatusLabel();
            labels.add(label);
            label.setVisible(false);
            this.add(label, "hidemode 3");
        }
        else {
            label = labels.get(labelsVisibleCount);
        }
        label.setVisible(true);
        label.setText(text, type);
        labelsVisibleCount++;
    }


    private boolean enabledInternal = true;

    @Override
    public void setEnabled(boolean enabled) {
        setOpaque(enabled);
        enabledInternal = enabled;
    }

    @Override
    public boolean isEnabled() {
        return enabledInternal;
    }

}   // StatusPanel
